package fm.last.api
{
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;
    
    import fm.last.Agent;
    import fm.last.RestAPI;
    
    final public class TrackAPI extends RestAPI
    {
        // those in comment are not implemented yet
        public static const TRACK_ADD_TAGS:String      = "track.addTags";
        public static const TRACK_BAN:String           = "track.ban";
        public static const TRACK_GET_INFO:String      = "track.getInfo";
        public static const TRACK_GET_SIMILAR:String   = "track.getSimilar";
        public static const TRACK_GET_TAGS:String      = "track.getTags";
        public static const TRACK_GET_TOP_FANS:String  = "track.getTopFans";
        public static const TRACK_GET_TOP_TAGS:String = "track.getTopTags";
        public static const TRACK_LOVE:String          = "track.love";
        public static const TRACK_REMOVE_TAG:String    = "track.removeTag";
        public static const TRACK_SEARCH:String        = "track.search";
        public static const TRACK_SHARE:String         = "track.share";
        
        public function TrackAPI(agent:Agent)
        {
            super(agent);
        }

        /* ********************************************************************/
        

        /** track.addTags */
        public function addTags(onComplete:Function, 
                                   onError:Function,                                                                     
                                   track:String,
                                   artist:String,
                                   tags:String):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_ADD_TAGS;
            urlVars["artist"] = artist;
            urlVars["track"]  = track;
            urlVars["tags"]   = tags;
            requestAPI(URLRequestMethod.POST, urlVars, true, onComplete, onError);        
        }
        
        
        /** track.ban */
        public function ban(onComplete:Function, 
                                   onError:Function,                                                                     
                                   track:String,
                                   artist:String):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_BAN;
            urlVars["artist"] = artist;
            urlVars["track"]  = track;
            requestAPI(URLRequestMethod.POST, urlVars, true, onComplete, onError);        
        }
        

        /** track.getInfo */
        public function getInfo(onComplete:Function, 
                                   onError:Function,
                                   track:String=null,                                  
                                   artist:String=null,                                   
                                   mbid:String=null):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_GET_INFO;
            if (artist!=null) urlVars["artist"] = artist;
            if (track!=null)  urlVars["track"] = track;
            if (mbid!=null)   urlVars["mbid"] = mbid;
            requestAPI(URLRequestMethod.GET, urlVars, false, onComplete, onError);        
        }
        

        /** track.getSimilar */
        public function getSimilar(onComplete:Function, 
                                   onError:Function,
                                   track:String=null,                                  
                                   artist:String=null,                                   
                                   mbid:String=null):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_GET_SIMILAR;
            if (artist!=null) urlVars["artist"] = artist;
            if (track!=null)  urlVars["track"] = track;
            if (mbid!=null)   urlVars["mbid"] = mbid;
            requestAPI(URLRequestMethod.GET, urlVars, false, onComplete, onError);        
        }
        

        /** track.getTags */
        public function getTags(onComplete:Function, 
                                   onError:Function,
                                   track:String,                                  
                                   artist:String):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_GET_TAGS;
            urlVars["artist"] = artist;
            urlVars["track"] = track;            
            requestAPI(URLRequestMethod.GET, urlVars, true, onComplete, onError);        
        }
        

        /** track.getTopFans */
        public function getTopFans(onComplete:Function, 
                                   onError:Function,
                                   track:String=null,                                  
                                   artist:String=null,                                   
                                   mbid:String=null):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_GET_TOP_FANS;
            if (artist!=null) urlVars["artist"] = artist;
            if (track!=null)  urlVars["track"] = track;
            if (mbid!=null)   urlVars["mbid"] = mbid;
            requestAPI(URLRequestMethod.GET, urlVars, false, onComplete, onError);        
        }
        

        /** track.getTopTags */
        public function getTopTags(onComplete:Function, 
                                   onError:Function,
                                   track:String=null,                                  
                                   artist:String=null,                                   
                                   mbid:String=null):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_GET_TOP_TAGS;
            if (artist!=null) urlVars["artist"] = artist;
            if (track!=null)  urlVars["track"] = track;
            if (mbid!=null)   urlVars["mbid"] = mbid;
            requestAPI(URLRequestMethod.GET, urlVars, false, onComplete, onError);        
        }
        

        /** track.love */
        public function love(onComplete:Function, 
                             onError:Function,
                             track:String,                                  
                             artist:String):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_LOVE;
            urlVars["artist"] = artist;
            urlVars["track"] = track;            
            requestAPI(URLRequestMethod.POST, urlVars, true, onComplete, onError);        
        }
        
        

        /** track.removeTag */
        public function removeTag(onComplete:Function, 
                             onError:Function,
                             track:String,                                  
                             artist:String,                             
                             tag:String):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_REMOVE_TAG;
            urlVars["artist"] = artist;
            urlVars["track"] = track;
            urlVars["tag"] = tag;            
            requestAPI(URLRequestMethod.POST, urlVars, true, onComplete, onError);        
        }
        

        /** track.search */
        public function search(onComplete:Function, 
                                   onError:Function,
                                   track:String,                                  
                                   artist:String=null,                                   
                                   page:int=-1,
                                   limit:int=-1):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_SEARCH;
            urlVars["track"] = track;            
            if (artist!=null) urlVars["artist"] = artist;            
            if (page>0)   urlVars["page"] = page;
            if (limit>0)   urlVars["limit"] = limit;
            requestAPI(URLRequestMethod.GET, urlVars, false, onComplete, onError);        
        }


        /** track.share */
        public function share(onComplete:Function, 
                             onError:Function,
                             track:String,                                  
                             artist:String, 
                             recipient:String,
                             message:String=null):void
        {
            var urlVars:URLVariables = new URLVariables();
            urlVars["method"] = TrackAPI.TRACK_SHARE;
            urlVars["artist"] = artist;
            urlVars["track"] = track;
            urlVars["recipient"] = recipient;
            if (message!=null) urlVars["message"] = message;            
            requestAPI(URLRequestMethod.POST, urlVars, true, onComplete, onError);        
        }
    }
}